﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Document_Management
{
    public partial class Cuttingtool_manager_seach1 : Form
    {
        bool b刀具Search, b刀具Insert, b刀具Edit, 刀具Del;
        TextBox tx;
        static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        SqlConnection con = new SqlConnection(connectionString);
        public Cuttingtool_manager_seach1(TextBox tx)
        {
            InitializeComponent();
            this.tx = tx;
        }

        public void Load_data(String proc)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(proc, con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dtGridmanagerSearch.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dtGridmanagerSearch.DataSource = dt;
                PublicClass.setRowNumber(dtGridmanagerSearch);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }
        private void Cuttingtool_manager_seach1_Load(object sender, EventArgs e)
        {
            dtGridmanagerSearch.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            if (tx.Name.ToString().Equals("txt保管人代號"))
            {
                Load_data("sp_刀具保管人_load");
            }
            if (tx.Name.ToString().Equals("txt領用人代號"))
            {
                Load_data("sp_刀具領用人_load");
            }
            if (tx.Name.ToString().Equals("cbx領用部門"))
            {
                Load_data("sp_刀具領用部門_load");
            }
        }

        private void bt_manager_確定_Click(object sender, EventArgs e)
        {
            try
            {
                int r = dtGridmanagerSearch.CurrentCell.RowIndex;
                string text = dtGridmanagerSearch.Rows[r].Cells[0].Value.ToString();
                tx.Text = text;
                this.Close();
            }
            catch
            {
                MessageBox.Show("資料不對");
            }
            
        }

        private void bt_manager_查詢_Click(object sender, EventArgs e)
        {
            if (!b刀具Search)
            {
                MessageBox.Show("很抱歉，您没有权限", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                string strcmd = "sp_刀具保管人_查詢";
                SqlDataAdapter cmd = new SqlDataAdapter(strcmd, con);
                cmd.SelectCommand.CommandType = CommandType.StoredProcedure;
                if (txtmanager查詢.Text != "") cmd.SelectCommand.Parameters.AddWithValue("@保管人代號或保管人姓名", txtmanager查詢.Text);
                DataTable dt = new DataTable();
                cmd.Fill(dt);
                dtGridmanagerSearch.DataSource = dt;
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
         
        }
        public void search(String sp_search)
        {
            try
            {
                con.Open();
                //string sp_search = "sp_車種_search";
                SqlDataAdapter cmd = new SqlDataAdapter(sp_search, con);
                cmd.SelectCommand.CommandType = CommandType.StoredProcedure;
                if (txtmanager查詢.Text != "") cmd.SelectCommand.Parameters.AddWithValue("@姓名或代號", txtmanager查詢.Text);
                DataTable dt = new DataTable();
                cmd.Fill(dt);
                dtGridmanagerSearch.DataSource = dt;
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
